//
//  CZBaseTableViewController.swift
//  Weibo
//
//  Created by 铭铭 on 15/11/23.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit

class CZBaseTableViewController: UITableViewController {
    
    //  用户登录的标志
    var userlogin = false
    
    override func loadView() {
        // 如果用户已经登录,正常加载TableView, 否则就创建访客视图
        userlogin ? super.loadView() : setupVistorVidew()
    }
    
    // 创建访客视图
    private func setupVistorVidew() {
        
      
        
         // 设置访客视图
        let vistorView = CZVistorView()
        
        view = vistorView
        
        // 遵守代理协议
        vistorView.VistorViewDelegate = self
        
        // MARK: - 监听方法
        /// 应用进入后台
        func didEnterBackground(){
              //房子动画暂停旋转
            vistorView.pauseAnimation()
            
        }
        //应用进入前台
        func didBecomeActive(){
          //房子动画恢复旋转
        vistorView.resumeAnimation()
        }
  

        //判断当前控制器
        if self is CZHomeViewController{
            
            vistorView.startRotationAnimation()
            
           // 监听应用退到后台
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "didEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
            // 监听应用退到前台
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "didBecomeActive", name: UIApplicationDidBecomeActiveNotification, object: nil)
            
        }else if self is CZMessageViewController{
            
          vistorView.setupInfo("visitordiscover_image_message", message: "登录后，别人评论你的微博，发给你的消息，都会在这里收到通知")
            
        }else if self is CZProfileViewController{
           
            vistorView.setupInfo("visitordiscover_image_message", message: "登录后，最新、最热微博尽在掌握，不再会与实事潮流擦肩而过")
            
        }else if self is CZDiscoverViewController{
             vistorView.setupInfo("visitordiscover_image_profile", message: "登录后，你的微博、相册、个人资料会显示在这里，展示给别人")
        }
       
        
        // 设置导航栏按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
        
         }

    //移除通知
    deinit{
        // 移除通知
        if self is CZHomeViewController {
            NSNotificationCenter.defaultCenter().removeObserver(self)
        }
        
    }
}

//按钮方法
extension CZBaseTableViewController: CZVistorViewDelegate{
    func VistorViewLoginClick() {
        
        let oauthVC = CZOauthViewController()
        
        presentViewController(UINavigationController(rootViewController: oauthVC), animated: true, completion: nil)
        
       print("CZBaseTableViewController:\(__FUNCTION__)")
    }
    func VistorViewRegisterClick() {
        
         print("CZBaseTableViewController:\(__FUNCTION__)")
    }
    
}

